package com.luyuan.crm.sale.web.converter;

import java.io.Serializable;

import javax.annotation.Resource;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import org.springframework.stereotype.Component;

import com.luyuan.crm.formula.model.Formula;
import com.luyuan.crm.formula.service.FormulaService;
import com.ruijie.web.jsf.converter.AbstractConverter;
import com.ruijie.util.StringUtils;

@Component("formulaConverter")
public class FormulaConverter extends AbstractConverter implements Converter,
		Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Resource(name = "formulaService")
	private FormulaService formulaService;

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {
		if (StringUtils.hasText(value)) {
			Formula model = formulaService.safeGet(value);
			return model;
		}
		return null;
	}

	@Override
	public String getAsString(FacesContext facesContext, UIComponent component,
			Object object) {
		if (object == null)
			return null;
		Formula model = (Formula) object;
		if (model.getId() == null) {
			return null;
		}
		return model.getId().toString();
	}

}
